DSC 40B

Problems tagged with "topological sorting"

Problem #267 🌶️

Tags: topological sorting

Consider the directed graph \(G = (V, E)\) with \(V = \{a, b, c\}\) and \(E = \{(a, b)\}\).

How many valid topological sorts of this graph are there?

Solution

3.

A topological sort is an ordering of the nodes in which every edge goes from an earlier node to a later one. The only edge is \((a, b)\), so the only rule is that \(a\) comes before \(b\). Node \(c\) has no edges at all, so it can go anywhere.

So we can start from the ordering \(a, b\) and put \(c\) in any of the three places: before \(a\), between \(a\) and \(b\), or after \(b\). That gives the three topological sorts

\[ c, a, b \qquad a, c, b \qquad a, b, c. \]

Another way to count them: there are \(3! = 6\) orderings of the three nodes, and \(a\) comes before \(b\) in exactly half of them.

Problem #268

Tags: topological sorting

Consider the directed acyclic graph \(G = (V, E)\) with \(V = \{a, b, c, d\}\) and \(E = \{(a, b), (a,c), (a,d)\}\). How many topological sorts of this graph are there?

Solution

6.

The edges say that \(a\) comes before \(b\), \(c\) and \(d\), so \(a\) must be first. There are no edges among \(b\), \(c\) and \(d\), so they can follow in any order: \(3! = 6\).

Problem #269

Tags: topological sorting

Let \(G = (V,E)\) be the directed graph with \(V = \{a, b, c, d, e\}\) and \(E = \{(a, b), (a, c), (a, d), (b, e), (c, e), (d, e)\}\). How many valid topological sorts of \(V\) are there?

Solution

Answer: 6.

Node \(a\) has an edge to each of \(b, c, d\), and each of them has an edge to \(e\). So \(a\) must be first and \(e\) last. There are no edges among \(b, c, d\), so they can go in the middle in any order: \(3! = 6\).

Problem #270

Tags: topological sorting

Find a topological sort of the graph shown below:

Solution

\(d, a, b, c, e\).

This is the only topological sort of this graph.

Problem #297

Tags: topological sorting

Suppose \(G\) is an acyclic directed graph. Let \(a\) and \(b\) be two nodes of \(G\) such that \(a\) appears before \(b\) in every possible topological sort of \(G\).

True or False: there must be a path from \(a\) to \(b\).

Solution

True.

Suppose there were no path from \(a\) to \(b\). Then \(a\) is not among the nodes that have a path to \(b\). We could list \(b\) and all the nodes with a path to \(b\) first (in topological order), followed by the rest. This is a valid topological sort with \(b\) before \(a\).